'use strict';

const Service = require('egg').Service;

function delArr(arr, id) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] === id) {
            arr.splice(i--, 1);
        }
    }
    return arr
}

function getResult(data) {
    let length = 0;
    for (var i = 0; i < data.length; i++) {
        length += parseInt(data[i].probability) //获取总数
    }
    for (var i = 0; i < data.length; i++) {
        var random = parseInt(Math.random() * length); //获取 0-总数 之间的一个随随机整数
        if (random < data[i].probability) {
            return data[i].prize //如果在当前的概率范围内,得到的就是当前概率
        } else {
            length -= data[i].probability //否则减去当前的概率范围,进入下一轮循环
        }
    }
}


class IndexService extends Service {
    //得到全部的用户
    async getAllPrize() {
        const data = await this.app.mysql.select('prize');
        return data
    };
    //中奖情况
    async getPrizeDesc() {
        const data = await this.app.mysql.select('lucky');
        return data
    };
    //奖品数目
    async getPrizeNum() {
            // const prizeArr = [1, 2, 3, 4, 5, 6, 7, 8, 7, 8, 6, 4, 7, 3, 4, 8, 3, 2, 4, 8];
            const prizeArr = [8, 7, 5, 4, 3, 6, 2, 1, 7, 8, 6, 4, 7, 3, 4, 8, 3, 2, 4, 8];
            var newArr = prizeArr
                // 查询奖品获取情况
            var day = new Date().getDate();
            const firstPrize = await this.app.mysql.select('lucky', { where: { prizeClass: '一等奖', day: day } });
            const accessit = await this.app.mysql.select('lucky', { where: { prizeClass: '二等奖', day: day } });
            const third = await this.app.mysql.select('lucky', { where: { prizeClass: '三等奖', day: day } });

            // const firstPrize = await this.app.mysql.query('select * from lucky where prizeClass = ?', ['一等奖']);
            // const accessit = await this.app.mysql.query('select * from lucky where prizeClass = ?', ['二等奖']);
            // const third = await this.app.mysql.query('select * from lucky where prizeClass = ?', ['三等奖']);

            //查询奖品数目
            const firstNum = await this.app.mysql.get('prize', { className: '一等奖' });
            const accessitNum = await this.app.mysql.get('prize', { className: '二等奖' });
            const thirdNum = await this.app.mysql.get('prize', { className: '三等奖' });
            if (firstPrize.length >= firstNum.number) {
                newArr = delArr(prizeArr, 1)
                newArr = delArr(prizeArr, 5)
            }
            if (accessit.length >= accessitNum.number) {
                newArr = delArr(prizeArr, 2)
                newArr = delArr(prizeArr, 6)
            }
            if (third.length >= thirdNum.number) {
                newArr = delArr(prizeArr, 3)
                newArr = delArr(prizeArr, 7)
            }
            return newArr
        }
        //九宫格
    async getSudoku() {
        const first = await this.app.mysql.get('prize', { id: 1 });
        const accessit = await this.app.mysql.get('prize', { id: 2 });
        const third = await this.app.mysql.get('prize', { id: 3 });
        const thank = await this.app.mysql.get('prize', { id: 4 });
        const res = [
            first,
            accessit,
            third,
            thank,
            first,
            accessit,
            third,
            thank
        ]
        return res
    };
    //记录用户抽奖记录
    async getLucky(obj) {
        obj.date = new Date();
        obj.day = new Date().getDate()
        const result = await this.app.mysql.insert('lucky', obj)
        if (result.affectedRows === 1) {
            return {
                code: 200,
                data: null,
                msg: '记录成功'
            }
        } else {
            return {
                code: 201,
                data: null,
                msg: '记录失败'
            }
        }
    };
    //查询用户是否能抽奖
    async getIsLucky(id) {
            var day = new Date().getDate();
            const data = await this.app.mysql.select('lucky', { where: { userId: id, day: day } });
            if (data.length >= 3) {
                return 0
            } else {
                return 3 - data.length
            }
        }
        //随机获取奖品
    async getluckyGood() {
        const data = await this.app.mysql.select('prize');

        // 查询奖品获取情况
        var day = new Date().getDate();
        const firstPrize = await this.app.mysql.select('lucky', { where: { prizeClass: '一等奖', day: day } });
        const accessit = await this.app.mysql.select('lucky', { where: { prizeClass: '二等奖', day: day } });
        const third = await this.app.mysql.select('lucky', { where: { prizeClass: '三等奖', day: day } });
        //查询奖品数目
        const firstNum = await this.app.mysql.get('prize', { className: '一等奖' });
        const accessitNum = await this.app.mysql.get('prize', { className: '二等奖' });
        const thirdNum = await this.app.mysql.get('prize', { className: '三等奖' });
        if (firstPrize.length >= firstNum.number) {
            for (let i = 0; i < data.length; i++) {
                if (data[i].className == '一等奖') {
                    data.splice(i, 1)
                }
            }
        }
        if (accessit.length >= accessitNum.number) {
            for (let i = 0; i < data.length; i++) {
                if (data[i].className == '二等奖') {
                    data.splice(i, 1)
                }
            }
        }
        if (third.length >= thirdNum.number) {
            for (let i = 0; i < data.length; i++) {
                if (data[i].className == '三等奖') {
                    data.splice(i, 1)
                }
            }
        }
        console.log(data)
        return getResult(data)
    }
}

module.exports = IndexService;